<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>${sysName}</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="${resRoot}/layui/css/layui.css" media="all" />
  <link rel="stylesheet" href="${resRoot}/layui/admin/admin.css" media="all" />
	<link id="layuicss-layer" rel="stylesheet" href="${resRoot}/layui/css/modules/layer/default/layer.css" media="all">
</head>
<body>
<div class="layui-fluid">   
          <div class="layui-card">
          <div class="layui-form layui-card-header layuiadmin-card-header-auto"> 
 
<form class="layui-form" action="#" style="margin-top: 20px;margin-bottom:50px;" lay-filter="dataForm"  >
<input type="hidden" id="id" name="id"  >
  <div class="layui-form-item">
    <label class="layui-form-label">组织名称</label>
    <div class="layui-input-inline">
	      <input type="text" name="name" id="name" value=""  autocomplete="off" placeholder="请输入组织名称" class="layui-input" lay-verify="required" lay-reqtext="请输入组织名称" >
    </div>
    <div class="layui-form-mid layui-word-aux">请输入组织名称</div>
  </div>

  <div class="layui-form-item">
    <label class="layui-form-label">组织编码</label>
    <div class="layui-input-inline">
	      <input type="text" name="code" id="code" value=""  autocomplete="off" placeholder="请输入组织编码" class="layui-input" lay-verify="required" lay-reqtext="请输入组织编码" >
    </div>
    <div class="layui-form-mid layui-word-aux">请输入组织编码</div>
  </div>

  <div class="layui-form-item">
    <label class="layui-form-label">上级组织</label>
    <div class="layui-input-inline">
	      <!-- <input type="text" name="parentId" id="parentId" value=""  autocomplete="off" placeholder="请输入上级组织" class="layui-input" lay-verify="required" lay-reqtext="请输入上级组织" > -->
	      <div id="ztree" class="ztree" >
			    </div>
	      <input id="parentId" name="parentId" type="hidden" >
    </div>
    <div class="layui-form-mid layui-word-aux">请输入上级组织</div>
  </div>

  <div class="layui-form-item">
    <label class="layui-form-label">组织类型</label>
    <div class="layui-input-inline">
	      <select name="type" id="type" lay-filter="typeChange"  lay-verify="required" lay-reqtext="请选择组织类型" >
        <option value="">请选择组织类型</option>
      </select>
    </div>
    <div class="layui-form-mid layui-word-aux">请输入组织类型</div>
  </div>

  <!--<div class="layui-form-item">
    <label class="layui-form-label">商家编码</label>
    <div class="layui-input-inline">
	      <input type="text" name="busId" id="busId" value="000"  autocomplete="off" placeholder="请输入商家编码" class="layui-input" lay-verify="required" lay-reqtext="请输入商家编码" >
    </div>
    <div class="layui-form-mid layui-word-aux">请输入商家编码</div>
  </div>-->

  <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn" id=""  lay-submit="" lay-filter="dataSaveSubmit"  type="button">立即提交</button>
      <button type="button" class="layui-btn layui-btn-primary" onclick="backList();" >返回</button>
    </div>
  </div>
</form>
</div>
  </div>
  </div>
<#include "/page/footer.html">
<link href="${resRoot}/jquery-ztree/3.5.40/css/zTreeStyle/zTreeStyle.css" rel="stylesheet" type="text/css"/>
<script src="${resRoot}/jquery-ztree/3.5.40/js/jquery.ztree.all.min.js" type="text/javascript"></script>
<script>
var id = "${id!}";

var parentId = "";
$(function(){
	setDictSelect("type", "dept_type", "");
});
function backList() {
	parent.layer.closeAll("iframe");
    //刷新父页面
    parent.location.reload();
}

function removeImg(id) {
	layer.confirm('您是确认删除/查看该图片吗？', {
		  btn: ['确认','查看'] //按钮
		}, function(){
			$("#" + id).attr("src", "");
			layer.msg('已删除！');
		}, function(){
			window.open($("#" + id).attr("src"));
		  //layer.msg('已取消！');
		});
}
layui.use(['form', 'layedit', 'laydate', 'upload'], function(){
  var form = layui.form
  ,layer = layui.layer
  ,layedit = layui.layedit
  ,laydate = layui.laydate;
  
  
  var setting = {check:{enable:true,chkStyle: "radio",radioType: "all",nocheckInherit:true},view:{selectedMulti:false},
			data:{simpleData:{enable:true,idKey: "id",pIdKey: "parentId",rootPId: 0}},callback:{beforeClick:function(id, node){
				tree.checkNode(node, !node.radio, true, true);
				return false;
			}}};

	var treeJson;
	var dataTmp = getDataById(id, "${base}/sys/dept/get/" + id)
	if(dataTmp) {
		parentId = dataTmp.parentId;
	}
	  $.ajax({
			type : "post",
			url : "${base}/sys/dept/getZTree",
			data : {},
			dataType : "json",
			async : false,
			success : function(data) {
				treeJson= data.message;
			}
		}); 
	// 用户-菜单
	var zNodes=treeJson;
	// 初始化树结构
	var tree = $.fn.zTree.init($("#ztree"), setting, zNodes);
	// 不选择父节点
	tree.setting.check.chkboxType = { "Y" : "ps", "N" : "s" };
	// 默认选择节点
	var node = tree.getNodeByParam("id", parentId);
	try{tree.checkNode(node, true, false);}catch(e){}
	// 默认展开全部节点
	tree.expandAll(true);
  
  
  var upload = layui.upload;
  
  // 页面验证，及自定义验证规则 样例 表单验证参考  https://www.layui.com/demo/form.html
  form.verify({
    title: function(value){
      if(value.length == 0){
        return '参数不能为空';
      }
    }
    ,pass: [/(.+){6,12}$/, '密码必须6到12位']
    ,content: function(value){
      layedit.sync(editIndex);
    }
  });
  
  
  form.on('submit(dataSaveSubmit)', function(data){
      var index = layer.msg('加载中...',{icon: 16,time:false,shade:0.8});
      var ids = "", nodes = tree.getCheckedNodes(true);
		for(var i=0; i<nodes.length; i++) {
			ids = nodes[i].id;
		}
		$("#parentId").val(ids);
		data.field.parentId = ids
		console.log(data.field)
      delete data.field.file;
      $.ajax({
			url : "${base}/sys/dept/save",
			contentType : "application/json",
         	dataType : 'json',
			data : JSON.stringify(data.field),
          	type:'post',
			async : true,
			success : function(data) {
				layer.close(index);
				console.log(data)
				if(data.code == 1) {
						layer.msg('保存成功！', {
						  icon: 1,
						  time: 2000 //2秒关闭（如果不配置，默认是3秒）
						}, function(){
							backList();
						});   
					} else {
						layer.msg(data.msg); 
					}
			}
		}); 
    });
  
});

layui.use('upload', function(){
	  
});  
</script>
</body>
</html>